import 'package:flower/model/address_list.dart';
import 'package:flutter/foundation.dart';


/* 地址管理状态 */
class AddressModel with ChangeNotifier, DiagnosticableTreeMixin {
  // 所有地址
  List<AddressData> allAddresses = [];

  // 默认地址
  AddressData? defaultAddress;

  // 当前选择的地址
  AddressData? selectedAddress;

  // 添加地址
  void addAddress(AddressData address) async {
    // // 添加到数据库
    // Map<String, dynamic> map = address.toJson();
    // final response = await HttpUtil.post(addAddressUrl, queryParameters: map);
    
    // if (response['success']) {
    //   // 更新本地状态
    //   allAddresses.add(address);
    //   if (address.isDefault == 1) {
    //     setDefaultAddress(address.id);
    //   }
    //   notifyListeners();
    // } else {
    //   // 处理错误
    // }
  }

  // 更新地址
  void updateAddress(AddressData address) async {
    // // 更新数据库
    // Map<String, dynamic> map = address.toJson();
    // final response = await HttpUtil.post(updateAddressUrl, queryParameters: map);
    
    // if (response['success']) {
    //   // 更新本地状态
    //   int index = allAddresses.indexWhere((a) => a.id == address.id);
    //   if (index != -1) {
    //     allAddresses[index] = address;
    //     if (address.isDefault == 1) {
    //       setDefaultAddress(address.id);
    //     }
    //     notifyListeners();
    //   }
    // } else {
    //   // 处理错误
    // }
  }

  // 删除地址
  void removeAddress(int id) async {
    // // 删除数据库中的地址
    // Map<String, dynamic> map = {"id": id};
    // final response = await HttpUtil.post(deleteAddressUrl, queryParameters: map);
    
    // if (response['success']) {
    //   // 更新本地状态
    //   allAddresses.removeWhere((address) => address.id == id);
    //   if (defaultAddress?.id == id) {
    //     defaultAddress = null;
    //   }
    //   if (selectedAddress?.id == id) {
    //     selectedAddress = null;
    //   }
    //   notifyListeners();
    // } else {
    //   // 处理错误
    // }
  }

  // 设置默认地址
  void setDefaultAddress(int id) async {
    for (var address in allAddresses) {
      if (address.id == id) {
        address.isDefault = 1;
        defaultAddress = address;
      } else {
        address.isDefault = 0;
      }
    }
    // 更新数据库

  }

  // 设置当前选择的地址
  void setSelectedAddress(int id) {
    selectedAddress = allAddresses.firstWhere((address) => address.id == id);
    notifyListeners();
  }

  // 获取当前选择的地址
  AddressData? getSelectedAddress() {
    return selectedAddress;
  }

  // 获取所有地址
  List<AddressData> getAllAddresses() {
    return allAddresses;
  }

  // 初始化地址数据
  void setAddressListData(List<AddressData> addressListData) {
    allAddresses = addressListData;
    for (var address in addressListData) {
      if (address.isDefault == 1) {
        defaultAddress = address;
        selectedAddress = address;
        break;
      }
    }
    notifyListeners();
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(IterableProperty<AddressData>('allAddresses', allAddresses));
    properties.add(DiagnosticsProperty<AddressData?>('defaultAddress', defaultAddress));
    properties.add(DiagnosticsProperty<AddressData?>('selectedAddress', selectedAddress));
  }
}
